#ifndef __WEBPAGEQUERY_H__
#define __WEBPAGEQUERY_H__

#include "WebPage.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <set>
//#include "include/cppjieba/Jieba.hpp"

using std::string;
using std::vector;
using std::unordered_map;
using std::pair;
using std::set;

class WebPageQuery{

    class AutoRelease{
    public:
        AutoRelease(){}

        ~AutoRelease(){
            if(_pInstance){
                delete _pInstance;
                _pInstance = NULL;
            }
        }
    };

public:
    WebPageQuery();
    ~WebPageQuery();

    string doQuery(const string &str);            //接收查询词条，执行查询，返回结果
    void loadLibrary();                           //加载库文件
    vector<double> getQueryWordsWeightVector      //计算查询词的权重
        (unordered_map<string, int> queryWords);
    bool executeQuery(const unordered_map<string, int> &queryWords,
        unordered_map<int, vector<double>> &resultVec);  //执行查询
    string createJson(vector<int> &docIdVec, unordered_map<string, int> &queryWords);
    string generateSummary(string &content, unordered_map<string, int> &queryWords);
    size_t nBytesCode(const char ch);
    static WebPageQuery *getInstance();

private:
    //cppjieba::Jieba* _pJieba;                        //Jieba分词库对象


    unordered_map<int, WebPage> _pageLib;             //网页库
    unordered_map<int, pair<int, int>> _offsetLib;    //偏移库
    unordered_map<string, vector<pair<int, double>>> _invertIndexTable;  //倒排索引表
    set<string> _stopWordList;                         //停用词集

    static AutoRelease _ar;
    static WebPageQuery *_pInstance;
    
};

#endif